//app.controller('userCtrl', function($scope, $http, $modal,$location, backendSrv, auth){
//app.controller('userCtrl',function ($scope, $modal, $log, auth, $location, backendSrv, $cookieStore) {
app.controller('userCtrl',function ($scope,  $modal, $log, auth, $location, backendSrv, $cookieStore) {
//app.controller('userCtrl',function ($scope, $log, auth, $location, backendSrv, $cookieStore) {

//    var $modal = {}
//    $scope.items = [ {id:0, name: 'item1'}, {id:1, name: 'item2'}, {id:2, name: 'item3'}, {id:3, name: 'item4'}, {id:4, name: 'item5'}, ];
   
    $scope.readCookie = function(){
        console.log($cookieStore.get('user'))
    } 
    $scope.role = function(){
    }

    $scope.userName = function(){
        var user = auth.getUser()
        return user ? user.Name : null 
    }
  
    $scope.login = function (size) {
  
      var modalInstance = $modal.open({
        templateUrl: 'part/loginModal.html',
        controller: 'ModalInstanceCtrl',
        size: size,
        resolve: {
          items: function () {
            return $scope.items;
          }
        }
      });
  
      modalInstance.result.then(function (selectedItem) {
         //TODO update authenticated user 
        //$scope.selected = selectedItem;
      }, function () {
        $log.info('Modal dismissed at: ' + new Date());
      });
    };


    $scope.hasRole = function(role){
        return auth.hasRole(role)
    }

    $scope.isLogged = function(role){
        return auth.getUser() ? true : false
    }
    
    $scope.logout = function(){
        backendSrv.sendCmd("/logout", null, function(data, status, headers, config){
            auth.setUser(null)
            $location.path("/main")
        }, null )
    }

    $scope.items = [ {id:0, name: 'item1'}, {id:1, name: 'item2'}, {id:2, name: 'item3'}, {id:3, name: 'item4'}, {id:4, name: 'item5'}, ];

    $scope.chosenItems = [$scope.items[2] ];
    $scope.notify = function(){
        $scope.chosenItems = [$scope.items[0]];
    }
    

    $scope.pointing = true;

});


app.controller('ModalInstanceCtrl', function ($scope, $http, $modalInstance, backendSrv, auth) {

    $scope.msg = " "
    $scope.user = {Email:"", Password:"", OrgEmail:""}
  
  
    var cb = function(data, status, headers, config){
        $scope.msg = data
        $modalInstance.close($scope.user);
        auth.setUser(data)
        
    }

    var cbErr = function(err){
        $scope.msg = "Error: " + err
    }

    $scope.ok = function () {
        $scope.msg = "Authenticating User..."
        backendSrv.sendCmd("/login", $scope.user, cb, cbErr )
    }
  
    $scope.cancel = function () {
      $modalInstance.dismiss('cancel');

    };


    

});
